package com.admin.dao;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.admin.bean.AdminUserBean;
import com.admin.bean.CompanyBean;

/**   
 * @Title: AdminUserDao.java
 * @Package com.admin.dao
 * @Description: 
 * @author Jason
 * @date 2010-12-14 ����10:16:04    
 * @version    
 */

@Repository
public class AdminUserDao {

	@Autowired private JdbcTemplate jdbcTemplate;
	
	public int addAdminUser(AdminUserBean bean){
		String sql = "insert into adminuser(username,pwd,power,cpname,cpmemo,cppercent,cpstatus,bussiness,cpercent,cpcheck,spchannel,mourl,mturl,createdtime) values(?,?,?,?,?,?,?,?,?,?,?,?,?,getDate())";
		return jdbcTemplate.update(sql, new Object[]{bean.getUsername(),bean.getPwd(),bean.getPower(),bean.getCpname(),bean.getCpmemo(),bean.getCppercent(),bean.getCpstatus(),bean.getBussiness(),bean.getCpercent(),bean.getCpcheck(),bean.getSpchannel(),bean.getMourl(),bean.getMturl()});
	}
	
	public AdminUserBean findByUserName(String userName){
		String sql = "select * from adminuser where username = ?";
		try{
			
			return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<AdminUserBean>(AdminUserBean.class), userName);
		}catch(RuntimeException e){
			e.printStackTrace();
			return null;
		}
	}
	
	public int updateAdminUser(AdminUserBean bean){
		final String sql = "update adminuser set cpname=?,cpmemo=?,cppercent=?,cpstatus=?,bussiness=?,cpercent=?,cpcheck=?,spchannel=?,mourl=?,mturl=? where id = ?";
		try {
			return jdbcTemplate.update(sql, new Object[]{bean.getCpname(),bean.getCpmemo(),bean.getCppercent(),bean.getCpstatus(),bean.getBussiness(),bean.getCpercent(),bean.getCpcheck(),bean.getSpchannel(),bean.getMourl(),bean.getMturl(),bean.getId()});
		} catch (DataAccessException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public AdminUserBean findById(int id){
		String sql = "select * from adminuser where id = ?";
		try{
			
			return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<AdminUserBean>(AdminUserBean.class), id);
		}catch(RuntimeException e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	public int findCount(){
		String sql = "select count(*) from adminuser";
		return jdbcTemplate.queryForInt(sql);
	}
	
	public List<AdminUserBean> findAll(int start,int pageSize){
		String sql = "select * from adminuser order by id desc";
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper<AdminUserBean>(AdminUserBean.class), new Object[]{});
	}
	
	public List<AdminUserBean> findCp(){
		String sql = "select * from adminuser where power = 0 order by id desc";
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper<AdminUserBean>(AdminUserBean.class), new Object[]{});
	}
	
	public List<AdminUserBean> findCp(String condition){
		String sql = "select * from adminuser where power = 0 and "+condition+" order by id desc";
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper<AdminUserBean>(AdminUserBean.class), new Object[]{});
	}
	
	public int updateUserPwd(int userId,String userPwd){
		String sql = "update adminuser set pwd = ? where id = ?";
		return jdbcTemplate.update(sql, new Object[]{userPwd,userId});
	}
	
	public int updateUserStatus(int userId,int status){
		String sql = "update adminuser set status = ? where id = ?";
		return jdbcTemplate.update(sql, new Object[]{status,userId});
	}
	
	
//	public int updateAdminUser(AdminUserBean bean){
//		String sql = "update adminuser set status = ? where id = ?";
//		return jdbcTemplate.update(sql, new Object[]{status,userId});
//	}
}
